<?php

namespace App\Http\Service;

use App\Http\Controllers\Admin\Traits\AdminTrait;
use App\Org\Dns;
use Illuminate\Support\Facades\Validator;
use App\Exceptions\CdjException;
use App\Exceptions\ApiException;
use App\Models\OperateLogs;
use App\Models\School\User;
use App\Models\Schools;
use Saas;


class SchoolsService
{
    use AdminTrait;

    public $operateData;
    public $schoolModel;
    public $action;

    public $token;
    public $schoolInfo;

    public $userInfo;

    public function __construct(Schools $schoolModel)
    {
        $this->schoolModel = $schoolModel;
    }

    /**
     * @param 学校列表
     * @param array $id
     * @return array
     * @throws CdjException
     */
    public function getList($params)
    {
        $list = Schools::list($params);
        foreach ($list['list'] as &$value) {
            $value['qualifications'] = json_decode($value['qualifications'], true);
            $value['created_at']     = date('Y-m-d H:i:s', $value['created_at']);
            $value['updated_at']     = date('Y-m-d H:i:s', $value['updated_at']);
        }

        return $list;
    }



    /**
     * @desc 数据处理
     * @param array $params
     * @return array
     * @throws CdjException
     */
    public function handler($params)
    {
        $this->action = $params['action'] ?? 'forbidden';
        switch ($this->action) {
            // 审核
            case "audit":
                $this->setAuditData($params);
                break;
            // 修改服务状态
            case "uptown":
                $validator = Validator::make($params, ['id' => 'required|numeric', 'status' => 'required|numeric',]);
                if ($validator->fails())
                    throw new CdjException($validator->errors()->first(), 40001);
                $info = Schools::where('id', $params['id'])->first();
                if (!$info)
                    throw new CdjException('学校信息不存在', 40002);
                Schools::where('id', $params['id'])->update(['status' => $params['status']]);
                break;
            default:
                throw new CdjException('非法操作', 50001);
        }

        return ['id' => $this->operateData['id'] ?? $params['id']];
    }

    private function setAuditData($params)
    {
        $rules = [
            'id'           => 'required|numeric',
            'audit_status' => 'required|numeric',
        ];

        $validator = Validator::make($params, $rules);
        if ($validator->fails())
            throw new CdjException($validator->errors()->first(), 40001);

        $info = Schools::where('id', $params['id'])->first();
        if (!$info)
            throw new CdjException('学校信息不存在', 40002);

        // 检测域名是否已解析
        $isExists = $this->domainIsExists($params['domain_name']);
        if (!$isExists)
            throw new CdjException('域名还未解析，请添加域名解析', 40002);

        $unixTime          = time();
        $this->operateData = [
            'id'           => $params['id'],
            'domain_name' => $params['domain_name'] ?? '',
            'audit_remark' => $params['audit_remark'] ?? '',
            'audit_status' => $params['audit_status'],
            'updated_at'   => $unixTime,
        ];

        return Schools::where('id', $params['id'])->update($this->operateData);
    }

    /**
     * @param 学校详情
     * @param array $id
     * @return array
     * @throws CdjException
     */
    public function getDetail($params)
    {
        if (!isset($params['id']) && !$params['id']) {
            throw new CdjException('学校ID不能为空', 40001);
        }

        $info = Schools::where('id', $params['id'])->first();
        if (!$info)
            throw new CdjException('学校信息不存在', 40002);
        $info->qualifications = json_decode($info->qualifications, true);

        return $info;
    }
}
